package com.tanhua.dubbo.server.api;


import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.UserLocation;
import com.tanhua.dubbo.server.vo.UserLocationVo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;


@Service(version = "1.0.0")
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String updateUserLocation(Long userId, Double longitude, Double latitude, String address) {
        UserLocation userLocation = new UserLocation();
        userLocation.setUserId(userId);
        userLocation.setLocation(new GeoJsonPoint(longitude,latitude));
        userLocation.setAddress(address);

        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation ul = this.mongoTemplate.findOne(query, UserLocation.class);

        if (ul == null){
//            新增
            long time = System.currentTimeMillis();
            userLocation.setId(new ObjectId());
            userLocation.setUpdated(time);
            userLocation.setCreated(time);
            userLocation.setLastUpdated(time);
            this.mongoTemplate.save(userLocation);
            return userLocation.getId().toHexString();
        }else {
//            更新
            Update update = Update
                    .update("location",userLocation.getLocation())
                    .set("update",System.currentTimeMillis())
                    .set("address",address)
                    .set("lastupdate",ul.getUpdated());
            this.mongoTemplate.updateFirst(query,update,UserLocation.class);
        }
        return ul.getId().toHexString();
    }

    @Override
    public UserLocationVo queryByUserId(Long userId) {
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation userLocation = this.mongoTemplate.findById(query, UserLocation.class);
            if (userLocation != null){
                UserLocationVo userLocationVo = UserLocationVo.format(userLocation);
                return userLocationVo;
            }
            return null;
    }

    @Override
    public List<UserLocationVo> queryUserFromLocation(Double longitude, Double latitude, Integer range) {
//        根据传入的位置确认中心点
        GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude,latitude);
//        画圆的半径
        Distance distance = new Distance(range / 1000, Metrics.KILOMETERS);
//        画一个圆圈
        Circle circle = new Circle(geoJsonPoint, distance);
//        查找在圆圈里的用户
        Query query = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = this.mongoTemplate.find(query, UserLocation.class);

        return UserLocationVo.formatToList(userLocations);
    }

}